package cn.tedu.egweibo04.controller;

import cn.tedu.egweibo04.base.response.JsonResult;
import cn.tedu.egweibo04.base.response.StatusCode;
import cn.tedu.egweibo04.mapper.WeiboMapper;
import cn.tedu.egweibo04.pojo.dto.WeiboDTO;
import cn.tedu.egweibo04.pojo.entity.Weibo;
import cn.tedu.egweibo04.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/v1/weibo")
@Api(tags = "02.微博管理模块")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @PostMapping("/insert")
    @ApiOperation("发布微博")
    @ApiImplicitParam(name = "weiboDTO", value = "微博数据", required = true, dataType = "WeiboDTO")
    public JsonResult insertWeibo(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) return new JsonResult(StatusCode.PUBLISH_FAILED);
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO, weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insertWeibo(weibo);
        return JsonResult.ok();
    }

    @GetMapping("/selectIndex")
    @ApiOperation("查询微博首页")
    public JsonResult selectIndex() {
        return JsonResult.ok(weiboMapper.selectIndex());
    }
@GetMapping("/selectById")
@ApiOperation("查询微博详情")
    public JsonResult selectDetail(Integer wid){
        return JsonResult.ok(weiboMapper.selectById(wid));
    }
}
